#ifndef _______________TERRAIN_PHONE__________ 
#define _______________TERRAIN_PHONE__________  1

half _TextureIntensity,_gama;
#if   ! _META_PASS

	#include "UnityCG.cginc"
	#include "Lighting.cginc"
	#include "AutoLight.cginc"

	#include "shadows.cginc"
 
#else
	#include "UnityCG.cginc"
	#include "UnityMetaPass.cginc"

#endif
			fixed4 _Color0;
			fixed4 _Color1;
			fixed4 _Color2;
			fixed4 _Color3;
			fixed4 _Color4;
			fixed4 _Color5;
			fixed4 _Color6;
			fixed4 _Color7;

			half4 _UVScale0123;
			half4 _UVScale4567;
			half4 _Gloss0123;
			half4 _Gloss4567;
			half4 _SpPower0123;
			half4 _SpPower4567;
			half4 _SpeColor;

			half _WorldNormalPower;
			UNITY_DECLARE_TEX2DARRAY(_MainTexArray);

			UNITY_DECLARE_TEX2DARRAY(_NormalTexArray);
			
			sampler2D _Control;
			sampler2D _Control2;
#if _NORMAL_MAP
			half4 _Normal0123;
			half4 _Normal4567;
 
#endif

#if _FLOWMAP
			sampler2D _FlowMap;
			
#endif
             
			half4 _TerrainEnvimentColor;
		 
			sampler2D _NoiseTex;
			half _NoiseForce;
			half2 _NoiseScale;
#if _WATER
			sampler2D _Sky;
			sampler2D _WaterNormal;
			sampler2D _Wave;
			half _SkyPower;
			half4 _ShallowColor;
			half4 _DeepColor;
			half4 _WaterParam;
			half4 _WaterParam2;
			half4 _WaterParam3;
			half _Fpow,_edgeFIntensity,_WaveTime;
			half3 _ViewCustom;
			float4 _Control2_TexelSize;
			
#endif
			
			inline half2 ToRadialCoordsNetEase(half3 envRefDir)
			{

				half k = envRefDir.x / (length(envRefDir.xz) + 1E-06f);
				half2 normalY = { k, envRefDir.y };
				half2 latitude = acos(normalY) * 0.3183099f;
				half s = step(envRefDir.z, 0.0f);
				half u = s - ((s * 2.0f - 1.0f) * (latitude.x * 0.5f));
				return half2(u, latitude.y);
			}
			//�����unity��
			inline float2 ToRadialCoords(float3 coords)
			{
				float3 normalizedCoords = normalize(coords);
				float latitude = acos(normalizedCoords.y);
				float longitude = atan2(normalizedCoords.z, normalizedCoords.x);
				float2 sphereCoords = float2(longitude, latitude) * float2(0.5 / UNITY_PI, 1.0 / UNITY_PI);
				return float2(0.5, 1.0) - sphereCoords;
			}

#if   ! _META_PASS

			
			 

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				half2 uv1 : TEXCOORD1;
				half3 normal : NORMAL;
				half4 tangent : TANGENT;
			};

			struct v2f
			{
				half4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;
				//UNITY_FOG_COORDS(1)
				#if _NORMAL_MAP
				half3 tspace0 : TEXCOORD2;
				half3 tspace1 : TEXCOORD3;
				half3 tspace2 : TEXCOORD4;

				#else
				half3 worldNormal : TEXCOORD2;
				#endif
				float3 worldPos : TEXCOORD5;

			

				//half4 ambientOrLightmapUV           : TEXCOORD2; // SH or Lightmap UV


				half4 ambientOrLightmapUV           : TEXCOORD6;
				SHADOW_COORDS(7)
 
			};

 
			half3 GetWorldNormal(v2f i, half3 n ,half3 MainNormal)
			{
#if _NORMAL_MAP
				n.xy = saturate(n.xy) * 2 - 1;
		 
				n.xy *= saturate(n.z);
				


				MainNormal.xy = saturate(MainNormal.xy) * 2 - 1;
		 
				MainNormal.xy *= saturate(MainNormal.z);

				MainNormal.z = sqrt(1 - saturate(dot(MainNormal.xy, MainNormal.xy)));
				//MainNormal.z = sqrt(1 - saturate(dot(MainNormal.xy, MainNormal.xy)));
				
				n = half3(n.xy+MainNormal.xy ,MainNormal.z);
				
				n = normalize(n);
				half3 normal;
				normal.x = dot(i.tspace0, n);
				normal.y = dot(i.tspace1, n);
				normal.z = dot(i.tspace2, n);
				normal = normalize(normal);
				return normal;
#else
				return  normalize(i.worldNormal);
#endif

			}
			
			v2f vert(appdata v)
			{
				v2f o;
				o.uv = v.uv;
				float4 worldPos =  mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1.0));
				o.pos = mul(UNITY_MATRIX_VP, worldPos);
				o.worldPos = worldPos.xyz;
				half3 normal = UnityObjectToWorldNormal(v.normal);
				#if _NORMAL_MAP
				
				half3 tangent = UnityObjectToWorldDir(v.tangent.xyz);
				half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
				half3 bitangent = cross(normal, tangent) * tangentSign;

				o.tspace0 = half3(tangent.x, bitangent.x, normal.x);
				o.tspace1 = half3(tangent.y, bitangent.y, normal.y);
				o.tspace2 = half3(tangent.z, bitangent.z, normal.z);

				#else
				o.worldNormal =  normal;
				#endif
 
				o.ambientOrLightmapUV = VertexGIForward(v.uv1, worldPos, normal);
		 
				TRANSFER_SHADOW(o);


				//UNITY_TRANSFER_FOG(o, o.pos);
				return o;
			}

			  float3 getNoise(float2 uv)
            {
                float2 scale = 10;
                float force = _NoiseForce;
                return tex2D(_NoiseTex, uv * _NoiseScale)/force;
            }

			
			fixed4 frag(v2f i) : SV_Target
			{
 
				half4 ctrl = tex2D(_Control, i.uv);
				half4 ctrl2 = tex2D(_Control2, i.uv);
				///
				float t0 = 1.0/(ctrl.r+ctrl.g+ctrl.b+ctrl.a + ctrl2.r+ctrl2.g+ctrl2.b+ctrl2.a);
				ctrl *= t0;
				ctrl2 *= t0;
				 
				half4 albedo = 0;
				half3 n = 0;
#if _NORMAL_MAP
				 
#endif

#if _IF_ENABLE
				if (ctrl.r > 0)
#endif
				

				{

 
					float3 _auv = float3(i.uv.xy*_UVScale0123.rr,0);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color0*ctrl.r;
					
					
#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, _auv ).xy, _Normal0123.x)*ctrl.r;
					 
#endif

				}
				
				
#if _IF_ENABLE
				if (ctrl.g > 0)
#endif
				
				{
					half3 _auv = float3(i.uv.xy*_UVScale0123.gg, 1);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color1*ctrl.g;
#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy,0) ).zw, _Normal0123.y)*ctrl.g;
#endif
				}
				
#if _IF_ENABLE
				if (ctrl.b > 0)
#endif	
				
				{
					half3 _auv = float3(i.uv.xy*_UVScale0123.bb, 2);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color2*ctrl.b;
#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy, 1)).xy, _Normal0123.b) *ctrl.b;
#endif
				}
	 
#if _IF_ENABLE
				if (ctrl.a > 0)
#endif
				
				{
					half3 _auv = float3(i.uv.xy*_UVScale0123.aa, 3);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color3*ctrl.a;
#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy, 1)).zw, _Normal0123.a)*ctrl.a;
#endif
				}
				 
#if _IF_ENABLE
				if (ctrl2.r > 0)
#endif
				
				{
					half3 _auv = float3(i.uv.xy*_UVScale4567.x, 4);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color4*ctrl2.r;

#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy, 2)).xy, _Normal4567.r)*ctrl2.r;
#endif
				}
#if _IF_ENABLE
				if (ctrl2.g > 0)
#endif
				
				{
					
					half3 _auv = float3(i.uv.xy*_UVScale4567.y, 5);
					albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*_Color5*ctrl2.g;
#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy, 2)).zw, _Normal4567.g)*ctrl2.g;
			 
#endif
				}
				half3 _auv7 = float3(i.uv.xy*_UVScale4567.z, 6);
#if _NORMAL_MAP
				//暂第七张世界法线
				
				half2 normal7 = UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv7.xy, 3)).xy;
 

#endif
#if _IF_ENABLE
				if (ctrl2.b > 0)
#endif
				
				{
					//half3 _auv = float3(i.uv.xy*_UVScale4567.z, 6);
					albedo += UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv7)*_Color6*ctrl2.b;
#if _NORMAL_MAP
					n.xyz += half3(normal7, _Normal4567.b)*ctrl2.b;
#endif
/*#if _NORMAL_MAP
					n.xyz += half3(UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, _auv ).xy, _Normal4567.b)*ctrl2.b;
#endif*/
				}
				half3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
				half3 normal  ;

#if _WATER
				 
#endif
				
#if _IF_ENABLE
				if (ctrl2.a > 0)
#endif
				
				{
					 

#if _WATER
					half4 waterColor = lerp(_ShallowColor, _DeepColor, ( ctrl2.a ));
					waterColor = saturate(waterColor);
					
#endif
					half3 _auv = float3(i.uv.xy*_UVScale4567.w, 7);
					half _wn = 0;
#if _NORMAL_MAP
					
	#if _WATER
					
					half2 _wc = 0;
					half _wt = 0;
					
		#if _WAVE 
		
					if (ctrl2.a < _WaterParam3.y && ctrl2.a > _WaterParam3.x)
					{
			 
						_wt = 1- ( (ctrl2.a - _WaterParam3.x) / (_WaterParam3.y - _WaterParam3.x));
						float ux =( (i.uv.x+i.uv.y)+  ( _Time.y *_WaveTime) ) *2;
						//return frac(ux);
						half2 _noizeUV = half2( ux ,_wt+getNoise(i.uv).x); //增加noise图平滑像素
						_wc.x = tex2D(_Wave, (_noizeUV.yx)).r;
 
						_wn = _wc.x * _WaterParam3.z ;

				
						    //   return _wt.xxxx .xxxx;
					}
                

				//	half4 ctrl2222 = (tex2D (_Control2, i.uv + _Control2_TexelSize.xy*0.6) +  tex2D (_Control2, i.uv - _Control2_TexelSize.xy*0.7)  )*0.5  ;
//half4 ctrl2222 = tex2Dlod (_Control2, i.uv) ;
				 

					//	return ctrl2.aaaa;
		#endif

					
		#if _FLOWMAP


					half2 _waterDir = tex2D(_FlowMap, i.uv.xy).xy;
					float2 flowDir = _waterDir * 2.0f - 1.0f;
					flowDir *= _WaterParam.x;

					float phase0 = frac(_Time.y* 0.5f + 0.5f);
					float phase1 = frac(_Time.y* 0.5f + 1.0f);
					half4 n3 = tex2D(_WaterNormal, (i.uv.xy + _Time.y*_WaterParam.zw )*_WaterParam2.y);
					half2 n1 = tex2D(_WaterNormal, (i.uv+ n3.xy*0.001 - flowDir.xy * phase0)*_WaterParam2.y).xy;
					half2 n2 = tex2D(_WaterNormal, (i.uv + n3.xy*0.001 - flowDir.xy * phase1)*_WaterParam2.y).xy;
 
					
					 

					float flowLerp = abs((0.5f - phase0) / 0.5f);
					half2 n0 = lerp(n1, n2, flowLerp);
					//n0 = (n0 + n3)*0.5;
 
		#else
					half4 n1 = tex2D(_WaterNormal, (i.uv.xy + _Time.y*_WaterParam.xy )*_WaterParam2.y);
					half4 n2 = tex2D(_WaterNormal, (i.uv.xy + _Time.y*_WaterParam.zw )*_WaterParam2.y);
					half2 n0 = (n1.xy + n2.xy)*0.5;
		#endif
			 
					n.xyz += half3(n0, _WaterParam2.z)*ctrl2.a;
			 

					
					_WorldNormalPower =  min(_WorldNormalPower ,(1- ctrl2.a));
				 
 
					//normal = GetWorldNormal(i, n, half3(normal7.xy, _WorldNormalPower));
					
	#else

					n.xyz += half3( UNITY_SAMPLE_TEX2DARRAY(_NormalTexArray, half3(_auv.xy, 3)).zw, _Normal4567.a)*ctrl2.a;
					 
	#endif
					
					//_auv.z = 11;
					


#endif
					//暂时第七张世界法线。
					//n = lerp(n, half3(normal7.xy,_Normal4567.b) , _WorldNormalPower);
					//n = lerp(n, half3(normal7.xy, _Normal4567.b), _WorldNormalPower);
					
					#if !_NORMAL_MAP
				//暂第七张世界法线
 
					half2 normal7 = n;
 

					#endif

					//return float4(normal7.xy*n.b,1,1);
					//n = lerp(n, half3(normal7.xy, _WorldNormalPower), 0.5);
					normal = GetWorldNormal(i, n ,half3(normal7.xy, _WorldNormalPower) );
					//albedo.rgb = half4(n.rg*n.b,0,1);

#if _WATER

	#if 1 //������׵ģ��е�覴ã�������̰������

					half3 _ref = reflect(viewDir, normal);
					half4 skyColor = tex2D(_Sky, ToRadialCoordsNetEase(_ref));
					
	#else
					half3 _ref = reflect(-viewDir, normal);
					half4 skyColor = tex2D(_Sky, ToRadialCoords(_ref));
	#endif
					
					 
					
					
					waterColor.rgb = lerp(waterColor.rgb, skyColor.rgb, saturate( _WaterParam2.x));
                   half3 v = _WorldSpaceCameraPos - i.worldPos;
				   v = normalize(v);
					waterColor.rgb  += (pow(dot(normal,normalize( _ViewCustom)),_Fpow) *_edgeFIntensity) *ctrl2.a  ;
			
					//	albedo += waterColor * ctrl2.a + _wn; //暂时隐藏浪花

					albedo +=  (waterColor * (ctrl2.a + _wn));

	 
#else
					
					albedo += ((UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv)))*_Color7*ctrl2.a;
#endif
					
				

					
					
				}



#if _IF_ENABLE
				else
				{
#if !_NORMAL_MAP
				//暂第七张世界法线

				half2 normal7 = n;


#endif
 
					//暂时第七张世界法线。
				 
					//n = lerp(n, half3(normal7.xy, _WorldNormalPower),0.5);
					//return float4(normal7.xy*n.b,1,1);
					normal = GetWorldNormal(i, n,half3(normal7.xy, _WorldNormalPower));
					
				}
#endif
				

				 
				
				 
				
				
				half3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
				fixed3 lightColor = _LightColor0;

				

	 

				

	#if defined(LIGHTMAP_ON)  
 

				half4 bakedColorTex = UNITY_SAMPLE_TEX2D(unity_Lightmap, i.ambientOrLightmapUV.xy);
				half3 ambient  = DecodeLightmap(bakedColorTex);
	#else

				//half3 ambient = i.ambientOrLightmapUV.rgb; 
				half3 ambient = 0;
	#endif 
			
				 
			
				
				half shadowMaskAttenuation = UnitySampleBakedOcclusion(i.ambientOrLightmapUV, 0);
				//return shadowMaskAttenuation;
				half realtimeShadowAttenuation = SHADOW_ATTENUATION(i);

				float zDist = dot(_WorldSpaceCameraPos - i.worldPos, UNITY_MATRIX_V[2].xyz);
				float fadeDist = UnityComputeShadowFadeDistance(i.worldPos, zDist);


				half atten = UnityMixRealtimeAndBakedShadows(realtimeShadowAttenuation, shadowMaskAttenuation, UnityComputeShadowFade(fadeDist));
			
			 

				half nl = saturate(dot(normal, lightDir));
				nl = pow(nl,1.5)  + nl;
			
				 
				half3 diffuse = (ambient + lightColor * nl *atten)* saturate( albedo.rgb);
				half4 c = 1;
				c.rgb = diffuse   ;
				#if _SPEC_ENABLE
				half nv = saturate(dot(normal, viewDir));
				half  _Gloss = dot(ctrl.rgba,  _Gloss0123.rgba ) + dot(ctrl2.rgba,  _Gloss4567  );
				half  _SpPower = dot(ctrl.rgba,  _SpPower0123.rgba ) + dot(ctrl2.rgba,  _SpPower4567 );
			 
				 
				half nDotH =  saturate ( dot(normal, normalize(viewDir + lightDir)) );
				
				half3 directSpe = pow( nDotH, (_Gloss    * 256))* _SpeColor  * max(0, albedo.a)*_SpPower;
 
				c.rgb += directSpe  ;
				#endif
			
				
 
			// c.rgb = pow(c.rgb,_gama) *_TextureIntensity;
			//c.rgb = c.rgb;
				// apply fog
				//UNITY_APPLY_FOG(i.fogCoord, c);
				return c;
			}





#else


	  
        struct v2f
        {
            float4 pos : SV_POSITION;
            float2 uv : TEXCOORD0;
 
 
            UNITY_VERTEX_OUTPUT_STEREO
        };

        //float4 _MainTex_ST;
        //float4 _Illum_ST;

        v2f vertMeta (appdata_full v)
        {
            v2f o;
            UNITY_SETUP_INSTANCE_ID(v);
            UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
            o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST);
            o.uv = v.texcoord;
   
        #ifdef EDITOR_VISUALIZATION
            o.vizUV = 0;
            o.lightCoord = 0;
            if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
                o.vizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, v.texcoord.xy, v.texcoord1.xy, v.texcoord2.xy, unity_EditorViz_Texture_ST);
            else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
            {
                o.vizUV = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
                o.lightCoord = mul(unity_EditorViz_WorldToLight, mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1)));
            }
        #endif
            return o;
        }

 
 

        half4 fragMeta (v2f i) : SV_Target
        {
            UnityMetaInput metaIN;
            UNITY_INITIALIZE_OUTPUT(UnityMetaInput, metaIN);

			half4 ctrl = tex2D(_Control, i.uv);
			half4 ctrl2 = tex2D(_Control2, i.uv);

			half4 albedo = 0;
 

#if _IF_ENABLE
			if (ctrl.r > 0)
#endif


			{

				half3 _auv = float3(i.uv.xy*_UVScale0123.rr, 0);
				
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl.r;

 

			}
#if _IF_ENABLE
			if (ctrl.g > 0)
#endif

			{
				half3 _auv = float3(i.uv.xy*_UVScale0123.gg, 1);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl.g;
 
			}
#if _IF_ENABLE
			if (ctrl.b > 0)
#endif	

			{
				half3 _auv = float3(i.uv.xy*_UVScale0123.bb, 2);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl.b;
 
			}
#if _IF_ENABLE
			if (ctrl.a > 0)
#endif

			{
				half3 _auv = float3(i.uv.xy*_UVScale0123.aa, 3);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl.a;
 
			}

#if _IF_ENABLE
			if (ctrl2.r > 0)
#endif

			{
				half3 _auv = float3(i.uv.xy*_UVScale4567.x, 4);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl2.r;
 
			}
#if _IF_ENABLE
			if (ctrl2.g > 0)
#endif

			{

				half3 _auv = float3(i.uv.xy*_UVScale4567.y, 5);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl2.g;
 
			}
#if _IF_ENABLE
			if (ctrl2.b > 0)
#endif

			{
				half3 _auv = float3(i.uv.xy*_UVScale4567.z, 6);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl2.b;
 
			}
#if _IF_ENABLE
			if (ctrl2.a > 0)
#endif

			{
				half3 _auv = float3(i.uv.xy*_UVScale4567.w, 7);
				albedo += (UNITY_SAMPLE_TEX2DARRAY(_MainTexArray, _auv))*ctrl2.a;
 
			}
            fixed4 c = albedo  ;
            metaIN.Albedo = c.rgb ;


            return UnityMetaFragment(metaIN);
        }

#endif

#endif
